<template>
  <el-dialog
    class="ba-operate-dialog"
    :close-on-click-modal="false"
    :model-value="baTable.form.operate ? true : false"
    @close="baTable.toggleForm"
  >
    <template #header>
      <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']">
        {{ baTable.form.operate !== "edit" ? "添加" : "编辑" }}
      </div>
    </template>
    <el-scrollbar
      v-loading="baTable.form.loading"
      class="ba-table-form-scrollbar"
    >
      <div
        class="ba-operate-form"
        :class="'ba-' + baTable.form.operate + '-form'"
        :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
      >
        <el-form
          ref="formRef"
          @keyup.enter="baTable.onSubmit(formRef)"
          :model="baTable.form.items"
          label-position="right"
          :label-width="baTable.form.labelWidth + 'px'"
          :rules="rules"
          v-if="!baTable.form.loading"
        >
          <el-form-item label="上级部门" prop="parentId">
            <el-tree-select
              v-model="baTable.form.items!.parentId"
              :data="state.deptOptions"
              check-strictly
              :props="{
                label: 'label',
                value: 'id',
                children: 'children',
              }"
              :render-after-expand="false"
            />
          </el-form-item>
          <el-form-item label="部门名称" prop="deptName">
            <el-input
              v-model="baTable.form.items!.deptName"
              placeholder="请输入部门名称"
            />
          </el-form-item>
          <!-- <el-form-item label="部门类型" prop="deptType">
            <el-select
              v-model="baTable.form.items!.deptType"
              placeholder="请选择部门类型"
            >
              <el-option
                v-for="item in dept.type"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue - 0"
              ></el-option>
            </el-select>
          </el-form-item> -->
          <el-form-item label="显示排序" prop="orderNum">
            <el-input-number
              v-model="baTable.form.items!.orderNum"
              controls-position="right"
              :min="0"
            />
          </el-form-item>

          <el-form-item label="负责人" prop="leader">
            <el-input
              v-model="baTable.form.items!.leader"
              placeholder="请输入负责人"
              maxlength="20"
            />
          </el-form-item>
          <el-form-item label="联系电话" prop="phone">
            <el-input
              v-model="baTable.form.items!.phone"
              placeholder="请输入联系电话"
              maxlength="11"
            />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input
              v-model="baTable.form.items!.email"
              placeholder="请输入邮箱"
              maxlength="50"
            />
          </el-form-item>
          <FormItem
            label="部门状态"
            v-model="baTable.form.items!.status"
            type="radio"
            :data="{
              content: { 0: '正常', 1: '停用' },
              childrenAttr: { border: true },
            }"
          />
        </el-form>
      </div>
    </el-scrollbar>
    <template #footer>
      <div
        :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'"
      >
        <el-button @click="baTable.toggleForm('')">取消</el-button>
        <el-button
          v-blur
          :loading="baTable.form.submitLoading"
          @click="baTable.onSubmit(formRef)"
          type="primary"
        >
          {{ baTable.form.operate === "itemAdd" ? "确定并新增下一项" : "确定" }}
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { reactive, ref, inject, onMounted } from "vue";
import type baTableClass from "@/utils/baTable";
import type { ElForm, FormItemRule, FormRules } from "element-plus";
import FormItem from "@/components/formItem/index.vue";
import { useUser } from "@/store/backend/SysM/user";
import { useDictionary } from "@/store/backend/tool/dictionary";
import { getDictData } from "@/utils/common"; // 导入表格api方法生成器

type Option = {
  id: number;
  label: string;
  children: Option[];
};
const user = useUser();
//接受来自父组件的注入
const baTable = inject("baTable") as baTableClass;
const formRef = ref<InstanceType<typeof ElForm>>();
const dictionary = inject("dictionary") as any;
const dept = inject("dept") as any;

const state: {
  deptOptions: Option[];
  deptTypeOptions: any[];
} = reactive({
  deptOptions: [], //部门列表
  deptTypeOptions: [], //部门类型列表
});
const rules = reactive<FormRules>({
  parentId: [{ required: true, message: "上级部门不能为空", trigger: "blur" }],
  deptName: [{ required: true, message: "部门名称不能为空", trigger: "blur" }],
  orderNum: [{ required: true, message: "显示排序不能为空", trigger: "blur" }],
  email: [
    {
      type: "email",
      message: "请输入正确的邮箱地址",
      trigger: ["blur", "change"],
    },
  ],
  phone: [
    {
      pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
      message: "请输入正确的手机号码",
      trigger: "blur",
    },
  ],
});

//获取工作中心/现部门类型字典
const getMenuStatus = () => {
  if (
    JSON.stringify(dictionary.moreTags) == "{}" ||
    !JSON.stringify(dictionary.moreTags["deptType"])
  ) {
    dictionary.getDictInfo("sys_workshop_type").then((res: any) => {
      dictionary.moreTags["deptType"] = getDictData(res);
      state.deptTypeOptions = res;
      console.log("获取工作中心类型的字典", state.deptTypeOptions);
    });
  }
};
//获取部门类型字典
getMenuStatus();

onMounted(() => {
  //获取岗位角色与部门三大列表数据
  dept.getDept().then((res: any) => {
    state.deptOptions = res.data;
    console.log("state.deptOptions", state.deptOptions);
  });
});
</script>
<style lang="scss" scoped></style>
